package com.leo.blog.aop;

import java.lang.reflect.Method;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.leo.blog.annotatiton.SystemLog;
import com.leo.blog.entity.Log;
import com.leo.blog.service.LogService;
import com.leo.blog.util.UserIpUtil;

@Aspect
@Component
public class SystemLogAspect {
	//注入Service用于把日志保存数据库
	@Autowired
	private LogService logService;
	
	//Service层切点
	@Pointcut("@annotation(com.leo.blog.annotatiton.SystemLog)")
	public void serviceAspect() {
	
	}
	
	@After("serviceAspect()")
	public void doServiceLog(JoinPoint joinPoint) {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
		try {
			//数据库日志
			Log log = new Log();
			//获取日志描述信息
			log.setLogUserType(getLogUserType(joinPoint));
			log.setLogIp(UserIpUtil.getIp(request));
			log.setLogParam(joinPoint.getArgs()[0].toString());
			log.setLogDescription(getLogDescription(joinPoint));
			log.setCreateTime(new Date());
			if(!log.getLogParam().equals("none")) {
				logService.addLog(log);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 获取注解中对方法的描述信息 用于service层注解
	 * @param joinPoint
	 *  切点
	 * @return
	 */
	private String getLogDescription(JoinPoint joinPoint) throws Exception{
		String targetName = joinPoint.getTarget().getClass().getName();
		String methodName = joinPoint.getSignature().getName();
		Object[] arguments = joinPoint.getArgs();
		Class<?> targetClass = Class.forName(targetName);
		Method[] methods = targetClass.getMethods();
		String logDescription = "";
		for(Method method : methods) {
			if(method.getName().equals(methodName)) {
				Class[] clazzs = method.getParameterTypes();
				if(clazzs.length == arguments.length) {
					logDescription = method.getAnnotation(SystemLog.class).description();
					break;
				}
			}
		}
		return logDescription;
	}
	
	/**
	 * 获取注解中的用户信息 用于service层注解
	 * @param joinPoint
	 * 切点
	 * @return
	 */
	private String getLogUserType(JoinPoint joinPoint) throws Exception{
		String targetName = joinPoint.getTarget().getClass().getName();
		String methodName = joinPoint.getSignature().getName();
		Object[] arguments = joinPoint.getArgs();
		Class<?> targetClass = Class.forName(targetName);
		Method[] methods = targetClass.getMethods();
		String logUserType = "";
		for(Method method : methods) {
			if(method.getName().equals(methodName)) {
				Class[] clazzs = method.getParameterTypes();
				if(clazzs.length == arguments.length) {
					logUserType = method.getAnnotation(SystemLog.class).userType();
					break;
				}
			}
		}
		return logUserType;
	}
}
